package main

import (
	"context"
	"os"
	"os/signal"
	"syscall"

	"gitee.com/go-course/go11/devcloud-mini/cmdb/logger"
	"gitee.com/go-course/go11/devcloud-mini/maudit/apps/log/provider/kafka"
	"gitee.com/go-course/go11/devcloud-mini/maudit/conf"
	"gitee.com/go-course/go11/devcloud-mini/maudit/exporter"
	"github.com/infraboard/mcube/ioc"

	// 加载所有的模块
	_ "gitee.com/go-course/go11/devcloud-mini/maudit/apps"
)

func main() {
	// 初始化配置
	conf.LoadConfigFromEnv()

	// 先初始化ioc
	err := ioc.InitIocObject()
	if err != nil {
		panic(err)
	}

	opp := kafka.NewKafkaOpeateLogProvider()
	ctx, cancel := context.WithCancel(context.Background())
	// 独立Goroutine跑在后台
	go opp.Run(ctx)

	exporter.BuildMetric()

	// 信号处理 负责阻塞住主进行(Loop Os Signal)
	// 处理信号量
	ch := make(chan os.Signal, 1)
	// 如果os发出了信号后，会把该信号发往 ch channal
	signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP, syscall.SIGQUIT)

	// 从chanel中获取信号
	s := <-ch
	logger.L().Info().Msgf("receive os signal: %s, exit ...", s)
	cancel()
}
